﻿using System;
using System.Collections.Generic;
using System.Text;
using Sinacor.Infra.Service.Security;
using System.ServiceModel;
using Sinacor.Service.Infra.AuthorizationService.Service.DataContract.Response;
using Sinacor.Infra.Service.Security.Services;

namespace Sinacor.Service.Infra.AuthorizationService.Service.ServiceImplementation
{
    [SinacorServiceBehavior]
	public class Policy : ServiceContract.IPolicy
    {
        #region Propriedades
        BusinessLogic.Policy _policyBL = new BusinessLogic.Policy();
        BusinessLogic.BehaviorPolicy _behaviorPolicyBL = new BusinessLogic.BehaviorPolicy();
        #endregion

        #region IPolicy Members

        /// <summary>
        /// Método responsável por retornar uma lista de Policy de acordo com os critérios de filtro
        /// Todas as propriedades do contrato RetrievePoliciesRequest são opcionais e podem ser utilizadas
        /// como filtro
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public List<DataContract.Response.PolicyResponse> RetrievePolicies(DataContract.Request.RetrievePoliciesRequest request)
        {
            BusinessEntity.Policy policyRequest;
            Translator.RetrievePoliciesRequestToPolicy translatorRetrievePoliciesRequest;
            Translator.PolicyToPolicyResponse translatorPolicy;

            translatorPolicy = new Translator.PolicyToPolicyResponse();
            translatorRetrievePoliciesRequest = new Translator.RetrievePoliciesRequestToPolicy();

            policyRequest = translatorRetrievePoliciesRequest.Translate(request);
            
            return translatorPolicy.TranslateToList(_policyBL.RetrievePolicies(policyRequest));                        
        }

        /// <summary>
        /// Método responsável por retornar uma Policy de acordo com o critério de filtro
        /// Todas as propriedades do RetrievePolicyRequest são obrigatórias e são utilizadas como filtro
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PolicyResponse RetrievePolicy(DataContract.Request.RetrievePolicyRequest request)
        {
            BusinessEntity.Policy policyRequest;
            Translator.RetrievePolicyRequestToPolicy translatorRetrievePolicyRequest;
            Translator.PolicyToPolicyResponse translatorPolicy;

            translatorPolicy = new Translator.PolicyToPolicyResponse();
            translatorRetrievePolicyRequest = new Translator.RetrievePolicyRequestToPolicy();

            policyRequest = translatorRetrievePolicyRequest.Translate(request);

            return translatorPolicy.Translate(_policyBL.RetrievePolicy(policyRequest));     
        }

        /// <summary>
        /// Método responsável por Incluir uma Policy
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public DataContract.Response.PolicyResponse InsertPolicy(DataContract.Request.InsertPolicyRequest request)
        {
            BusinessEntity.Policy policyRequest;
            Translator.InsertPolicyRequestToPolicy translatorInsertPolicyRequest;
            Translator.PolicyToPolicyResponse translatorPolicy;

            translatorInsertPolicyRequest = new Translator.InsertPolicyRequestToPolicy();

            translatorPolicy = new Translator.PolicyToPolicyResponse();

            policyRequest = translatorInsertPolicyRequest.Translate(request);

            return translatorPolicy.Translate(_policyBL.InsertPolicy(policyRequest));
        }


        /// <summary>
        /// Método responsável por Alterar uma Policy
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public void UpdatePolicy(DataContract.Request.UpdatePolicyRequest request)
        {
            BusinessEntity.Policy policyRequest;
            Translator.UpdatePolicyRequestToPolicy translatorUpdatePolicyRequest;

            translatorUpdatePolicyRequest = new Translator.UpdatePolicyRequestToPolicy();

            policyRequest = translatorUpdatePolicyRequest.Translate(request);

            _policyBL.UpdatePolicy(policyRequest);
        }

        /// <summary>
        /// Método responsável por Excluir uma Policy
        /// </summary>
        /// <param name="request"></param>
        public void DeletePolicy(DataContract.Request.DeletePolicyRequest request)
        {
            BusinessEntity.Policy policyRequest;
            Translator.DeletePolicyRequestToPolicy translatorDeletePolicyRequest;

            translatorDeletePolicyRequest = new Translator.DeletePolicyRequestToPolicy();

            policyRequest = translatorDeletePolicyRequest.Translate(request);

            _policyBL.DeletePolicy(policyRequest);
        }

        /// <summary>
        /// Metodo responsavel por retornar a lista de Behavior Policies do usuário autenticado
        /// Retornando as Behavior Policies as quais o usuario tem acesso e também as que ele não tem (Behavior Policy default)
        /// </summary>
        /// <returns></returns>
        public List<BehaviorPolicyResponse> RetrieveUserBehaviorPolicies(DataContract.Request.RetrieveUserBehaviorPoliciesRequest request)
        {
            Translator.BehaviorPolicyToBehaviorPolicyResponse 
                translator = new Translator.BehaviorPolicyToBehaviorPolicyResponse();

            Translator.RetrieveUserBehaviorPoliciesRequestToBehaviorPolicy
                translatorRetrieveUserBehaviorPoliciesRequest = new Translator.RetrieveUserBehaviorPoliciesRequestToBehaviorPolicy();

            BusinessEntity.BehaviorPolicy behaviorPolicyRequest = translatorRetrieveUserBehaviorPoliciesRequest.Translate(request);
            
            return translator.TranslateToList(_behaviorPolicyBL.RetrieveUserBehaviorPolicies(behaviorPolicyRequest,request.ModuleId));
        }


        /// <summary>
        /// Método responsável por retornar uma lista de BehaviorPolicy de acordo com os critérios de filtro
        /// Todas as propriedades do contrato RetrieveBehaviorPoliciesRequest são opcionais e podem ser utilizadas
        /// como filtro 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public List<BehaviorPolicyResponse> RetrieveBehaviorPolicies(DataContract.Request.RetrieveBehaviorPoliciesRequest request)
        {
            BusinessEntity.BehaviorPolicy behaviorPolicyRequest;
            Translator.RetrieveBehaviorPoliciesRequestToBehaviorPolicy translatorRetrieveBehaviorPoliciesRequest;
            Translator.BehaviorPolicyToBehaviorPolicyResponse translatorBehaviorPolicy;

            translatorBehaviorPolicy = new Translator.BehaviorPolicyToBehaviorPolicyResponse();
            translatorRetrieveBehaviorPoliciesRequest = new Translator.RetrieveBehaviorPoliciesRequestToBehaviorPolicy();

            behaviorPolicyRequest = translatorRetrieveBehaviorPoliciesRequest.Translate(request);

            return translatorBehaviorPolicy.TranslateToList(_behaviorPolicyBL.RetrieveBehaviorPolicies(behaviorPolicyRequest));    
        }

        /// <summary>
        /// <summary>
        /// Método responsável por retornar uma BehaviorPolicy de acordo com o critério de filtro
        /// Todas as propriedades do RetrieveBehaviorPolicyRequest são obrigatórias e são utilizadas como filtro
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public BehaviorPolicyResponse RetrieveBehaviorPolicy(DataContract.Request.RetrieveBehaviorPolicyRequest request)
        {
            BusinessEntity.BehaviorPolicy behaviorPolicyRequest;
            Translator.RetrieveBehaviorPolicyRequestToBehaviorPolicy translatorRetrieveBehaviorPolicyRequest;
            Translator.BehaviorPolicyToBehaviorPolicyResponse translatorBehaviorPolicy;

            translatorBehaviorPolicy = new Translator.BehaviorPolicyToBehaviorPolicyResponse();
            translatorRetrieveBehaviorPolicyRequest = new Translator.RetrieveBehaviorPolicyRequestToBehaviorPolicy();

            behaviorPolicyRequest = translatorRetrieveBehaviorPolicyRequest.Translate(request);

            return translatorBehaviorPolicy.Translate(_behaviorPolicyBL.RetrieveBehaviorPolicy(behaviorPolicyRequest));     
        }

        /// <summary>
        /// Método responsável por Incluir uma BehaviorPolicy
        /// </summary>
        /// <param name="request"></param>
        public void InsertBehaviorPolicy(DataContract.Request.InsertBehaviorPolicyRequest request)
        {
            BusinessEntity.BehaviorPolicy behaviorPolicyRequest;
            Translator.InsertBehaviorPolicyRequestToBehaviorPolicy translatorInsertBehaviorPolicyRequest;

            translatorInsertBehaviorPolicyRequest = new Translator.InsertBehaviorPolicyRequestToBehaviorPolicy();

            behaviorPolicyRequest = translatorInsertBehaviorPolicyRequest.Translate(request);

            _behaviorPolicyBL.InsertBehaviorPolicy(behaviorPolicyRequest);
        }

        /// <summary>
        /// Método responsável por Atualizar uma BehaviorPolicy
        /// </summary>
        /// <param name="request"></param>
        public void UpdateBehaviorPolicy(DataContract.Request.UpdateBehaviorPolicyRequest request)
        {
            BusinessEntity.BehaviorPolicy behaviorPolicyRequest;
            Translator.UpdateBehaviorPolicyRequestToBehaviorPolicy translatorUpdateBehaviorPolicyRequest;

            translatorUpdateBehaviorPolicyRequest = new Translator.UpdateBehaviorPolicyRequestToBehaviorPolicy();

            behaviorPolicyRequest = translatorUpdateBehaviorPolicyRequest.Translate(request);

            _behaviorPolicyBL.UpdateBehaviorPolicy(behaviorPolicyRequest);
        }

        /// <summary>
        /// Método responsável por Excluir uma BehaviorPolicy
        /// </summary>
        /// <param name="request"></param>
        public void DeleteBehaviorPolicy(DataContract.Request.DeleteBehaviorPolicyRequest request)
        {
            BusinessEntity.BehaviorPolicy behaviorPolicyRequest;
            Translator.DeleteBehaviorPolicyRequestToBehaviorPolicy translatorDeleteBehaviorPolicyRequest;

            translatorDeleteBehaviorPolicyRequest = new Translator.DeleteBehaviorPolicyRequestToBehaviorPolicy();

            behaviorPolicyRequest = translatorDeleteBehaviorPolicyRequest.Translate(request);

            _behaviorPolicyBL.DeleteBehaviorPolicy(behaviorPolicyRequest);
        }
        #endregion
    }
}